package com.letter.otools.day;

import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;

import androidx.appcompat.app.ActionBar;

import android.os.Bundle;

import androidx.appcompat.widget.Toolbar;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.letter.otools.day.R;
import com.letter.otools.day.admob.AdmobBannerActivity;
import com.letter.otools.day.util.AppUtils;

import org.litepal.LitePal;

import java.util.Calendar;

public class AddItemActivity extends AdmobBannerActivity {

    public static final int ITEM_ADD = 0;
    public static final int ITEM_EDIT = 1;

    private int editType;

    private Calendar selectCalender = Calendar.getInstance();

    private TextView textYear;
    private TextView textMouth;
    private TextView textDay;

    private ActionBar actionBar;

    private EditText editTextName;

    private LinearLayout dateChoose;

    private DatePickerDialog datePickerDialog;

    private AlertDialog.Builder typeDialog;

    private LinearLayout typeLayout;

    private TextView textType;

    private Anniversary anniversary;

    private FrameLayout adViewContainer;

    @Override
    protected int getLayout() {
        return R.layout.activity_add_item;
    }

    @Override
    protected FrameLayout getAdContainer() {
        return adViewContainer;
    }

    @Override
    protected void initBinder() {
        super.initBinder();
        adViewContainer = findViewById(R.id.ad_view_container);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_add_item);
        Toolbar toolbar = findViewById(R.id.Toolbar);
        setSupportActionBar(toolbar);
//        this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

        actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(AppUtils.getNightMode(this) == MODE_NIGHT_YES ? R.drawable.ic_toolbar_back_white_24dp : R.drawable.ic_toolbar_back_gray_24dp);
            actionBar.setTitle("");
        }

        Intent intent = getIntent();
        editType = intent.getIntExtra("editType", ITEM_ADD);
        if (editType == ITEM_EDIT) {
            anniversary = LitePal.find(Anniversary.class, intent.getIntExtra("anniId", 0));
        } else {
            anniversary = new Anniversary();
        }

        editTextName = findViewById(R.id.text_name);
        editTextName.setText(anniversary.getText());

        dateChoose = findViewById(R.id.date_choose);
        selectCalender.setTimeInMillis(anniversary.getTime());
        freshDate(selectCalender);

        datePickerDialog = new DatePickerDialog(AddItemActivity.this);
        datePickerDialog.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int mouth, int day) {
                selectCalender.set(year, mouth, day);
                anniversary.setTime(selectCalender.getTimeInMillis());
                freshDate(selectCalender);
            }
        });

        dateChoose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                datePickerDialog.show();
            }
        });

        textType = findViewById(R.id.type);
        textType.setText(Anniversary.typeText[anniversary.getType()]);

        typeDialog = new AlertDialog.Builder(this).setTitle("Day type")
                .setItems(Anniversary.typeText, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        textType.setText(Anniversary.typeText[i]);
                        anniversary.setType(i);
                    }
                });


        typeLayout = findViewById(R.id.type_layout);
        typeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                typeDialog.show();
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.add_item_toolbar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent = new Intent();
        int itemId = item.getItemId();
        if (itemId == android.R.id.home) {
            setResult(RESULT_CANCELED, intent);
            finish();
        } else if (itemId == R.id.save) {
            EditText editText = findViewById(R.id.text_name);
            EditText contentInput = findViewById(R.id.contentInput);
            if (editText.getText().toString().equals("")) {
                Toast.makeText(AddItemActivity.this, "anniversary name cannot be empty", Toast.LENGTH_SHORT).show();
                return true;
            }
            anniversary.setText(editText.getText().toString());
            anniversary.setContent(contentInput.getText().toString());
            if (editType == ITEM_EDIT) {
//                    anniversary.setTime(anniversary.getTime() + 1);       //可解决不能修改时间为当前日期的bug
                anniversary.update(anniversary.getId());
            } else {
                anniversary.save();
            }
            Intent broadIntent = new Intent("android.appwidget.action.APPWIDGET_UPDATE");
            getApplicationContext().sendBroadcast(broadIntent);
            Toast.makeText(AddItemActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
            setResult(RESULT_OK, intent);
            finish();
        }
        return true;
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        setResult(RESULT_CANCELED, intent);
        finish();
    }

    private void freshDate(Calendar calendar) {
        textYear = findViewById(R.id.text_year);
        textMouth = findViewById(R.id.text_mouth);
        textDay = findViewById(R.id.text_day);

        textYear.setText(String.valueOf(calendar.get(Calendar.YEAR)));
        textMouth.setText(String.valueOf(calendar.get(Calendar.MONTH) + 1));
        textDay.setText(String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));
    }

}
